<%@page import="javax.xml.soap.Detail"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<%
         request.setCharacterEncoding("UTF-8");
%>
<%@ page import="java.sql.*" %>

<html>
<head>
  		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
                <title>delete</title>
		  <style type="text/css">
                       body{
                           background-image: url('photo/delete2.jpg');
                           background-size:cover;  
                         }
                    </style>
</head>
<body scroll = no>
<%
Statement sql;
Connection con;
ResultSet rs;
   try{
       Class.forName("com.mysql.jdbc.Driver");
       }
       catch(Exception e){
    	   out.print("驱动失败");
       };
       
       
    	try{ 
    
     		 String uri="jdbc:mysql://localhost:3306/sharing";
          
    	   con = DriverManager.getConnection(uri,"root","ZHUBI123.");
    	   int id = Integer.parseInt(request.getParameter("id"));//获得主贴id
    	   int replyid = Integer.parseInt(request.getParameter("replyid"));//删除主贴还是回复贴
    	   if(replyid>0){
	    	   if (session.getAttribute("username").equals("admin")) 
		  {
        	   String updatecondition = "delete from reply "
     				+ "  where reply=" + id + " and id =" + replyid;//删除回复贴
              	  sql=con.createStatement();
     		  sql.executeUpdate(updatecondition);
               	  out.print("帖子已经删除");
	 	  response.sendRedirect("Detail.jsp?id=" + id);
    	   	  }
    	   	  else
    		   out.print("<h4>"+"You have no right");%>
      		     <a href=Detail.jsp?id=<%=id%>>return</a>
    		   <% 
    	   }
       	
	else
         {

        if (session.getValue("username").equals("admin"))
        {
        String  condition3 = "delete from discuss where id=" + id;
          sql=con.createStatement();
                sql.executeUpdate(condition3);
                String condition2= "delete from reply where reply=" + id;
                sql=con.createStatement();
                sql.executeUpdate(condition2);
                String condition4 = "alter table discuss drop id ";
                sql= con.createStatement();
                        sql.executeUpdate(condition4);
                        String condition5 = "alter table discuss add id int not null auto_increment primary key";
                        sql= con.createStatement();
                        sql.executeUpdate(condition5);
          response.sendRedirect("discuss.jsp");
        }
        else
                out.print("<h4>"+"You have no right");%>
		</br>
		<h4>only admin can delete! </h4>
		<p>账号:admin 密码:123</p>
               <a href=discuss.jsp>return</a>
	<%
        }
        
       }
    	catch(Exception e){
    		out.print("发生错误");
    	}
     %>
    	   </body></html>
